package test.orwapkg;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCConnector {
	public static void main(String args[]){
		String url = "jdbc:oracle:thin:@localhost:1521:xe";
		String username = "hr";
		String password = "hr"; 
		try (Connection conn = DriverManager.getConnection(url, username, password)){
			//Statement stm = conn.createStatement();
			Statement stm = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
			System.out.println("Connection successful!!!");
 			ResultSet rs = stm.executeQuery("select sysdate from dual");
 			rs.next();
 			Date date = rs.getDate("sysdate");
 			System.out.println(date);
 			
 			stm = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
 			rs = stm.executeQuery("select employee_id, first_name, LAST_NAME, manager_id, email, HIRE_DATE, JOB_ID  from employees where salary > 9000");
 			
 			while(rs.next()){
 				//System.out.println("inside loop");
 				//System.out.println(rs.getString("FIRST_NAME"));
 				if(rs.getString("FIRST_NAME").equals("Steven")){
 					//System.out.println("inside if");
 					rs.updateString("EMAIL", "SKING2");
 					rs.updateRow();
 					conn.commit();
 				}
 			}
 			
 			date = Date.valueOf("2009-4-12");
 			rs.moveToInsertRow();
 			rs.updateString("EMPLOYEE_ID", "207");
 			rs.updateString("LAST_NAME", "Badran");
 			rs.updateString("EMAIL", "badran.orwa@gmail.com");
 			rs.updateDate("HIRE_DATE", date);
 			rs.updateString("JOB_ID", "AC_MGR");
 			rs.insertRow();
 			conn.commit();
 			
 			//PreparedStatement ps = conn.pre
 			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
